package com.slipper.system.organization.controller;

import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import com.slipper.util.Tool;
import jakarta.annotation.Resource;

import com.slipper.constants.Constants;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.organization.bo.OrganizationBO;
import com.slipper.system.organization.service.OrganizationService;
import com.slipper.util.ResultUtil;

import java.util.List;

/**
 * 组织机构信息表
 */
@RestController
@RequestMapping("/system/organization")
public class OrganizationController {
    @Resource
    private OrganizationService organizationService;

    @PostMapping("/queryBy")
    public String queryBy(@RequestBody OrganizationBO bo) {
        return ResultUtil.successResult(organizationService.queryBy(bo.getId()));
    }

    @PostMapping("/queryPage")
    public String queryPage(@RequestBody OrganizationBO bo) {
        return ResultUtil.successResult(organizationService.queryPage(bo));
    }

    @PostMapping("/queryPageValid")
    public String queryPageValid(@RequestBody OrganizationBO bo) {
        bo.setValid(Constants.VALID);
        return ResultUtil.successResult(organizationService.queryPage(bo));
    }

    @PostMapping("/queryTree")
    public String queryTree() {
        List<OrganizationBO> organizationList = organizationService.queryList(new OrganizationBO());
        TreeInfo treeInfo = new TreeInfo("组织机构树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, organizationList, "asc"));
    }

    @PostMapping("/save")
    public String save(@RequestBody OrganizationBO bo) {
        return ResultUtil.saveResult(organizationService.save(bo));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody OrganizationBO bo) {
        String[] ids = null;
        if (bo.getIds() != null) {
            ids = bo.getIds();
        } else if (bo.getId() != null) {
            ids = new String[] { bo.getId() };
        }
        if (ids == null) {
            return ResultUtil.failureResult("参数不正确！");
        }

        return ResultUtil.deleteResult(organizationService.delete(bo));
    }

    @PostMapping("/updateValid")
    public String updateValid(@RequestBody OrganizationBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数不正确！");
        }

        bo.setValid(Constants.YES);
        return ResultUtil.saveResult(organizationService.updateValid(bo));
    }

    @PostMapping("/updateInValid")
    public String updateInValid(@RequestBody OrganizationBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数不正确！");
        }

        bo.setValid(Constants.NO);
        return ResultUtil.saveResult(organizationService.updateValid(bo));
    }
}
